package com.li.service;

import com.li.dao.UserMapper;

import com.li.pojo.User;
import com.li.util.MyTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAllUserById(List<String> uids) {
        return userMapper.selectAllUserById(uids);
    }

    @Override
    public User checkAddUser(String name) {
        User user = userMapper.checkAddUser(name);
        return user;
    }

    @Override
    public long count(String id ,String key) {
        int newId;
        String newKey;
        if (key!=null&&!"".equals(key)){
            newKey="%"+key+"%";
        }else {
            newKey=null;
        }

        if (id!=null&&!"".equals(id)){
            newId=Integer.parseInt(id);
        }else {
            newId=0;
        }

        return userMapper.count(newId,newKey);
    }

    @Override
    public  List<User> selectUsers(String id ,String key,int page,int pageSize) {
        int newId;
        String newKey;

        if (key!=null&&!"".equals(key)){
            newKey="%"+key+"%";
        }else {
            newKey=null;
        }

        if (id!=null&&!"".equals(id)){
            newId=Integer.parseInt(id);
        }else {
            newId=0;
        }
        return userMapper.selectUsers(newId,newKey,(page-1)*pageSize,pageSize);
    }

    @Override
    public int addUser(User user) {
        if (this.checkAddUser(user.getName())!=null){
            return 0;
        }
        user.setId(0);
        user.setIsDelete("0");
        userMapper.addUser(user);
        return 1;
    }
    public int deleteUsers(List<Integer> id){
        return userMapper.deleteUsers(id);
    }


    @Override
    public User login(String account, String password) {
        User user = userMapper.login(account,password);
        if (user!=null){
            user.setLastLogin(MyTimeUtil.getCurrentTime());
            userMapper.updateUser(user);
        }
        return user;
    }


    public User selectOne(int id){
        return userMapper.selectOne(id);
    }


    public int updateUser(User user){
        if (user==null){
            return 0;
        }
        return userMapper.updateUser(user);
    }
    public List<User> selectAllUsers(){
        return userMapper.selectAllUsers();
    }
}
